<template>
  <div class="SearchResult-wrap">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item,index) in list" :key="index"  :title="item.title" @click="gotoArticle(item)"/>
    </van-list>
  </div>
</template>
<script>
import { searchResult } from '@/api/user.js'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1, // 当前页码
      perPage: 10 // 当前页条数
    }
  },
  props: {
    // 搜索输入框内的内容
    searchText: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad () {
      // 1.请求数据
      const params = {
        page: this.page,
        per_page: this.perPage,
        q: this.searchText
      }
      const { data } = await searchResult(params)
      /* 2、将数据放到list数组中 */
      const { results } = data.data
      this.list.push(...results)
      // 3 加载状态结束
      this.loading = false
      // 4 判断是否还有数据
      if (results.length) {
        // 有数据就请求下一页
        this.page++
      } else {
        // 结束触发加载更多
        this.finished = true
      }
    },
    // 去文章页面  article
    gotoArticle (item) {
      this.$router.push({
        path: '/article',
        query: item
      })
    }
  }
}
</script>
<style scoped lang='less'>
.SearchResult-wrap {
  position: fixed;
  top: 1.26667rem;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
}
</style>
